<?php


class AddressDAO {
  
    
    public function __construct($db) {
        
        $this->db=$db;
    }
    
    
  public function create(ContactAddress $ad) {
		$db = $this->db;
		
		$stmt = $db->prepare("INSERT INTO `address` (
                    `street`, `number`, `neigh`, `zipcod`, `city`, `state`, `idClients`) VALUES (?,?,?,?,?,?,?)");
		
                $street = $ad->getStreet();
                $stmt->bindParam(1, $street);
		
		$number = $ad->getNumber();
                $stmt->bindParam(2, $number );
		
                $neigh= $ad->getNeigh();
                $stmt->bindParam(3, $neigh);
                
                $zipcod=$ad->getZipcod();
                $stmt->bindParam(4,$zipcod);
               
                $city= $ad->getCity();
                $stmt->bindParam(5, $city);
                
                $state= $ad->getState();
                $stmt->bindParam(6, $state);
                
                $idClients= $ad->getIdClients();
                $stmt->bindParam(7, $idClients);
                
                
                $result=$stmt->execute();
               if($result){
                   return true;
               }else return 0;
               
	}
	
	public function update(ContactAddress $ad) {
		$db = $this->db;
                $idCli=$ad->getIdClients();
                ECHO "IDCLI>$idCli";
		
		$stmt = $db->prepare("update address a set 
                    a.street=?,a.number=?, a.neigh=?, a.zipcod=?, a.city=?, a.state=? where a.idClients=$idCli ");
		
                $street = $ad->getStreet();
                $stmt->bindParam(1, $street);
		
		$number = $ad->getNumber();
                $stmt->bindParam(2, $number );
		
                $neigh= $ad->getNeigh();
                $stmt->bindParam(3, $neigh);
                
                $zipcod=$ad->getZipcod();
                $stmt->bindParam(4,$zipcod);
               
                $city= $ad->getCity();
                $stmt->bindParam(5, $city);
                
                $state= $ad->getState();
                $stmt->bindParam(6, $state);
                
                
                
                
                $result=$stmt->execute();
               if($result){
                   return true;
               }else {
                   echo $result;
                   return 0;}
               
	}
	
	public function remove($id) {
		
	}
	
	

        
        public function getByIdCli($id) {
		
		$db = $this->db;
		
		$stmt = $db->prepare("SELECT * FROM address a WHERe a.idClients = $id ");
                
		$stmt->execute();
                $row = $stmt->fetch();
		
		if(isset($row['idClients'])) {
			
			$ad = new ContactAddress();
                        $ad->setStreet($row['street']);
                        $ad->setNumber($row['number']);
                        $ad->setNeigh($row['neigh']);
                        $ad->setZipcod($row['zipcod']);
                        $ad->setCity($row['city']);
                        $ad->setState($row['state']);
                        
		}
		
		return $ad;
	}  
    
            
            





}



?>
